package com.usian.controller;

import com.usian.feign.ItemServiceFeign;
import com.usian.pojo.TbItemParam;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 韩耀东
 * @date 2021/1/6 10:36
 */
@RestController
@RequestMapping("/backend/itemParam")
public class ItemParamController {
    @Autowired
    private ItemServiceFeign itemServiceFeign;

    /**
     * @Author 韩耀东
     * @Description //TODO 根据商品分类 ID 查询规格参数模板
     * @Date 2021/1/6 10:38
     * @Param
     * @return
     **/
    @RequestMapping("/selectItemParamByItemCatId/{itemCatId}")
    public Result selectItemParamByItemCatId(@PathVariable("itemCatId") Long itemCatId){
        TbItemParam tbItemParam = itemServiceFeign.selectItemParamByItemCatId(itemCatId);
        if (tbItemParam == null) {
            return Result.error("未查到，该记录");
        }
        return Result.ok(tbItemParam);
    }

    /**
     * @Author 韩耀东
     * @Description //TODO 查询商品规格
     * @Date 2021/1/7 19:51
     * @Param [page, rows]
     * @return com.usian.utils.Result
     **/
    @RequestMapping("/selectItemParamAll")
    public Result selectItemParamAll(@RequestParam(defaultValue = "1") Integer
                                             page,@RequestParam(defaultValue = "5") Integer rows){
        PageResult pageResult =
                this.itemServiceFeign.selectItemParamAll(page,rows);
        if(pageResult.getResult().size() > 0){
            return Result.ok(pageResult);
        }
        return Result.error ("查无结果");
    }

    @RequestMapping("/insertItemParam")
    public Result insertItemParam(Long itemCatId,String paramData){
        Integer integer = itemServiceFeign.insertItemParam(itemCatId, paramData);
        if (integer == 0) {
            return Result.error("添加失败");
        }
        return Result.ok();
    }

    @RequestMapping("/deleteItemParamById")
    public Result deleteItemParamById(Long id){
        Integer integer = itemServiceFeign.deleteItemParamById(id);
        if (integer == 0) {
            return Result.error("删除失败");
        }
        return Result.ok();
    }

}
